<?php
class My_Db_Page {
	private $total_page;
	private $total_nombre;
	private $page_courante;
	private $_request;
	
	function getPage($total_nombre,$request,$paginal = 10) {
		$this->_request = clone $request;
		$this->page_courante = $this->_request->getParam ( "page" );
		$this->total_page = ceil ( $total_nombre / $paginal );
		$this->total_nombre = $total_nombre;
	}
	
	function render() {
		$html = '';
		if ($this->total_page > 1) {
			for($i = 0; $i < $this->total_page; $i ++) {
				if ($i > 0)
					$html .= "/";
				$this->_request->setParam ( "page", $i );
				$html .= '<a href="' . $this->rebuildUrl () . '">' . ($i + 1) . '</a>';
			}
		}
		return $html;
	}
	function toArray() {
		if ($this->total_page > 1) {
			for($i = 0; $i < $this->total_page; $i ++) {
				$this->_request->setParam ( "page", $i );
				$arr [$i] ["url"] = $this->rebuildUrl ();
				$arr [$i] ["name"] = ($i + 1);
			}
		}
		return $arr;
	}
	
	function rebuildUrl() {
		$url = $this->_request->getBaseUrl ();
		$params = $this->_request->getParams ();
		$url .= "/" . $params ["module"];
		$url .= "/" . $params ["controller"];
		$url .= "/" . $params ["action"];
		foreach ( $params as $key => $value ) {
			if (! in_array ( $key, array ("module", "controller", "action" ) ))
				$url .= "/" . $key . "/" . $value;
		}
		return $url;
	}
}

?>